package com.sd.repay.jedis.impl;

import com.sd.repay.jedis.JedisClient;
import com.sd.repay.utils.ObjectTranscoder;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.Map;

public class JedisClientSingle implements JedisClient {

    @Autowired
    private JedisPool jedisPool;

    @Override
    public String get(String key) {
        Jedis jedis = jedisPool.getResource();
        String string = jedis.get(key);
        jedis.close();
        return string;
    }

    @Override
    public String set(String key, String value) {
        Jedis jedis = jedisPool.getResource();
        String string = jedis.set(key, value);
        jedis.close();
        return string;
    }

    @Override
    public String set(String key, String value, int second) {
        Jedis jedis = jedisPool.getResource();
        String string = jedis.set(key, value);
        if (second > 0) {
            jedis.expire(key, second);
        }
        jedis.close();
        return string;
    }

    @Override
    public String hget(String hkey, String key) {
        Jedis jedis = jedisPool.getResource();
        String string = jedis.hget(hkey, key);
        jedis.close();
        return string;
    }

    @Override
    public long hset(String hkey, String key, String value) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.hset(hkey, key, value);
        jedis.close();
        return result;
    }

    @Override
    public long hset(String hkey, String key, String value, int second) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.hset(hkey, key, value);
        if (second > 0) {
            jedis.expire(hkey, second);
        }
        jedis.close();
        return result;
    }

    @Override
    public long hset(String hkey, Map<String, String> map, int second) {
        if (map == null || map.isEmpty())
            return 0L;
        Jedis jedis = jedisPool.getResource();
        for (Map.Entry<String, String> key : map.entrySet()) {
            jedis.hset(hkey, key.getKey(), key.getValue());
        }
        if (second > 0) {
            jedis.expire(hkey, second);
        }
        jedis.close();
        return 1;
    }

    @Override
    public Map<String, String> hget(String hkey) {
        Jedis jedis = jedisPool.getResource();
        Map<String, String> res = jedis.hgetAll(hkey);
        jedis.close();
        return res;
    }

    @Override
    public long incr(String key) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.incr(key);
        jedis.close();
        return result;
    }

    @Override
    public long expire(String key, int second) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.expire(key, second);
        jedis.close();
        return result;
    }

    @Override
    public long ttl(String key) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.ttl(key);
        jedis.close();
        return result;
    }

    @Override
    public long del(String key) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.del(key);
        jedis.close();
        return result;
    }

    @Override
    public long hdel(String hkey, String key) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.hdel(hkey, key);
        jedis.close();
        return result;
    }

    @Override
    public boolean exists(String key) {
        Jedis jedis = jedisPool.getResource();
        boolean result = jedis.exists(key);
        jedis.close();
        return result;
    }

    /**
     * 设置 list
     *
     * @param <T>
     * @param key
     * @param
     */
    @Override
    public <T> void setList(String key, List<T> list) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.set(key.getBytes(), ObjectTranscoder.serialize(list));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }

    @Override
    public <T> void setList(String key, List<T> list, int second) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.set(key.getBytes(), ObjectTranscoder.serialize(list));
            jedis.expire(key.getBytes(), second);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }

    /**
     * 获取list
     *
     * @param <T>
     * @param key
     * @return list
     */
    @Override
    public <T> List<T> getList(String key) {
        Jedis jedis = jedisPool.getResource();
        byte[] in = jedis.get(key.getBytes());
        List<T> list = (List<T>) ObjectTranscoder.deserialize(in);
        jedis.close();
        return list;
    }

    /**
     * 设置 map
     *
     * @param <T>
     * @param key
     * @param
     */
    @Override
    public <T> void setMap(String key, Map<String, T> map) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.set(key.getBytes(), ObjectTranscoder.serialize(map));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }

    @Override
    public <T> void setMap(String key, Map<String, T> map, int second) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.set(key.getBytes(), ObjectTranscoder.serialize(map));
            jedis.expire(key.getBytes(), second);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }

    /**
     * 获取map
     *
     * @param <T>
     * @param key
     * @return
     */
    @Override
    public <T> Map<String, T> getMap(String key) {
        Jedis jedis = jedisPool.getResource();
        byte[] in = jedis.get(key.getBytes());
        Map<String, T> map = (Map<String, T>) ObjectTranscoder.deserialize(in);
        jedis.close();
        return map;
    }
}
